<?php
/**
 * Created by PhpStorm.
 * User: lwt
 * Date: 5/15/19
 * Time: 4:24 PM
 */

namespace app\admin\controller;

use think\Request;
use app\admin\model\BatchModel;


class Batch
{
	/**
     * 添加批次信息
     * @return \think\response\Json
     */
    public function addBatch(){
    	$request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $data["ba_time"] = time();
        $msg = BatchModel::addBatch($data);
        if ($msg == 0) {
            $content["code"] = 400;
            $content["msg"] = "商品不存在！";
            return json($content);
        } else {
            $content["code"] = 200;
            $content["msg"] = "批次添加成功！";
            return json($content);
        }
    }

    /**
     * 批量刪除批次信息
     * @return \think\response\Json
     */
    public function deleteBatch(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $coIdList = explode(",",$data["co_id"]);
        $num = count($coIdList);
        $deleteNum = 0;

        $msg = array();
        for ($x=0; $x<$num; $x++){
            $a = BatchModel::deleteBatch($coIdList[$x]);
            if ($a == 0){
                $msg["code"] = 400;
                $msg["deleteNum"] = "已刪除".$deleteNum."條，id：".$coIdList[$x]."不存在";
                $msg["msg"] = "刪除異常！";
                return json($msg);
            }

            $deleteNum = $deleteNum + $a;
        }
        $msg["code"] = 200;
        $msg["deleteNum"] = "已刪除".$deleteNum."條";
        $msg["msg"] = "刪除成功！";

        return json($msg);
    }

    /**
     * 更新批次信息
     * @return \think\response\Json
     */
    public function updateBatch(){
		$request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $upload = BatchModel::updateBatch($data);
        if ($upload == 0) {
            $content["code"] = 400;
            $content["msg"] = "更改失败！请检查批次ID是否存在！！";
            return json($content);
        } else {
            $content["code"] = 200;
            $content["msg"] = "更改成功！";
            return json($content);
        }
    }

    /**
     * 根據商品id查詢商品对应批次信息
     * @return \think\response\Json
     */
    public function selectBatchById(){
        $request = Request::instance();
        $data = $request->param();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $content = array();
        $data = BatchModel::selectBatchById($data["cos_id"]);
        $content["code"] = 200;
        $content["num"] = count($data);
        $content["msg"] = $data;
        return json($content);
    }
}